<template>
  <div v-show="!Object.keys(config).length" style="margin-bottom: 16px"></div>
  <div v-show="Object.keys(config).length" :class="['banner',!questionList.length && 'active']">
    <h1 class="title">{{ config.bannerTitle }}</h1>
    <h3 class="subhead" v-html="config.bannerText"></h3>
    <img class="bannerImg" :src="config.bannerImg" alt="">
  </div>
  <div class="recommend" v-if="questionList.length">
    <div class="question">
      <h2 class="title">你想了解什么呢？</h2>
      <div class="change" @click="change">
        <span class="text">换一批</span>
        <img class="img" src="../assets/image/change.png" alt="">
      </div>
    </div>
    <ul class="questionList">
      <li class="item" v-for="v in questionList" :key="v.id" @click="getItem(v)">
        {{v}}
      </li>
    </ul>
  </div>

</template>

<script setup>
import { ref,reactive, onMounted } from 'vue';
import { debounce } from 'lodash-es';
import {getProjectConfig, getQuestionList} from "@/utils/api.js";
const emit = defineEmits(['getQuestion'])

const questionList = ref([])
const config = ref({})

let hotNum = ref(0)

function formatWord(text){
  return text.replace(/\\n/g,'<br>')
}
function getHotQuestionList(){
  getQuestionList({num:5}).then(v=>{
    if(v.c === "100"){
      questionList.value = v.d
    }else {
      console.log(v.d)
    }
  }).catch(err =>{
    if(err.response.status === 404) return;
    if(err.response.status === 500 && hotNum.value >= 2){
      alert('连接失败')
      return
    }else if(err.response.status === 502){
      console.log("服务器故障")
    }else {
      // getHotQuestionList()
      if( hotNum.value >= 2){
        return;
      }
    }
    hotNum.value++
  })
}




const getItem = debounce((text)=>{
  emit('getQuestion',text)
},100)

const change = debounce(() =>{
  getHotQuestionList()
},100)

onMounted(()=>{
 setTimeout(()=>{
   const info = JSON.parse(localStorage.getItem('config') )
   config.value = info
   config.value.bannerText =  formatWord(info.bannerText || '')
 },50)
  getHotQuestionList()
})

</script>

<style lang="scss" scoped>
.banner{
  margin: 30px 0 0;
  width: 100%;
  height: 230px;
  background:linear-gradient(339deg, rgba(255, 255, 255, 0.9) 60%, #C3DDFF 100%);
  padding: 40px 0 0 25px;
  border: 1px solid #FFFFFF;
  box-sizing: border-box;
  border-radius: 10px;
  display: flex;
  position: relative;
  flex-direction: column;
  .title{
    line-height: 42px;
    font-family: Bold,serif;
    font-size: 32px;
    color: #006FFF;
  }
  .subhead{
    width: 520px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; //设置行数
    overflow: hidden; //超出隐藏
    font-family: Regular,serif;
    font-weight: 400;
    font-size: 26px;
    margin-top: 36px;
    line-height: 42px;
    color: #6F8394;
  }
  .bannerImg{
    position: absolute;
    top: 0;
    right: 0;
    width: 150px;
    height: 230px;
  }
  &.active{
    margin-bottom: 30px;
  }
}
.recommend{
  margin: 40px auto 30px;
  width: 690px;
  padding: 30px 30px 10px;
  box-sizing: border-box;
  border-radius: 15px;
  background: #fff;
  .question{
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title{
      font-family: Bold,serif;
      font-size: 28px;
      color: #3D3D3D;
      position: relative;
      z-index: 1;
      &::after{
        position: absolute;
        left: 0 ;
        bottom: -5px;
        z-index: -1;
        content: '';
        display: inline-block;
        width: 210px;
        height: 10px;
        border-radius: 5px;
        background: linear-gradient(88deg, #197CFD 0%, #00FFA2 100%);
      }
    }
    .change{
      display: flex;
      align-items: center;
      .text{
        font-family: Regular,serif;
        font-size: 24px;
        color: #6F8394;
        margin-left: 10px;
        margin-right: 6px;
      }
      .img{
        width: 26px;
        height: 26px;
      }
    }
  }
  .questionList{
    margin-top: 10px;
    .item{
      font-family: Regular,serif;
      font-size: 28px;
      width: 100%;
      padding:26px 0 ;
      line-height: 44px;
      display: flex;
      align-items: center;
      color: #3D3D3D;
      position: relative;
      box-sizing: border-box;
      &::after{
        position: absolute;
        bottom: 1px;
        left: 0;
        content: '';
        display: inline-block;
        width: 100%;
        height: 1px;
        background: #F0F0F0;
      }
      &:last-child::after{
        display: none;
      }
    }
  }
}

</style>